package com.park.controller;

import com.park.common.bean.OrderInputView;
import com.park.common.bean.PageBean;
import com.park.common.bean.ResponseBean;
import com.park.common.exception.MessageException;
import com.park.common.poo.ExpressInfo;
import com.park.common.poo.OrderDetail;
import com.park.common.poo.OrderInfo;
import com.park.common.poo.UserOperator;
import com.park.common.util.JsonUtils;
import com.park.platform.service.IAreaService;
import com.park.platform.service.IExpressService;
import com.park.platform.service.ITmOrderService;
import com.park.wx.util.QrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("order")
public class TmOrderController extends BaseController {

    @Autowired
    private ITmOrderService orderService;

    @Autowired
    private IExpressService expressService;

    @Autowired
    private IAreaService areaService;

    @RequestMapping("toOrder")
    public String toOrder(int activityId, Model model){
        try {
            UserOperator userInfo = getUserInfo2();
            Map<String, Object> data = orderService.toOrder(activityId, userInfo != null ? userInfo.getId() : null);
            model.addAllAttributes(data);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "order";
    }

    @ResponseBody
    @RequestMapping("newOrder")
    public ResponseBean newOrderInfo(OrderInfo orderInfo, OrderDetail orderDetail, HttpServletResponse response){
        try{
            UserOperator userInfo = getUserInfo2();
            if(userInfo == null) throw new MessageException("请您先登录");
            orderInfo.setId(userInfo.getId());
            orderService.saveOrderInfo(orderInfo, orderDetail);
            String codeUrl = orderService.getCodeurl(orderInfo.getOrderId());

            Map data = new HashMap();
            data.put("orderId", orderInfo.getOrderId());
            data.put("codeUrl", codeUrl);
            data.put("payStatus", orderInfo.getPayStatus()); //是否是0元，0元则直接支付成功
            return new ResponseBean(data);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @RequestMapping("wxPayCode")
    public void wxPayPackage(String codeUrl, HttpServletResponse response){
        try {
            QrUtil.initQr(codeUrl, 300, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("asynNotify")
    public void asynNotify(HttpServletRequest request, HttpServletResponse response) throws Exception{
        try{
            response.setContentType("text/xml");
            String msgxml = this.getStreamResult(request, response);//xml数据
            orderService.asynNotify(msgxml, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping("getOrderPayStatus")
    public ResponseBean getOrderPayStatus(int orderId){
        try{
            Map<String, Object> data = new HashMap<>();
            data.put("ps", orderService.getOrderPayStatus(orderId));
            return new ResponseBean(data);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("getExpress")
    public ResponseBean getExpress(Integer expressId){
        try{
            UserOperator userInfo = getUserInfo2();
            Map<String, Object> data = new HashMap<>();
            if(expressId != null) {
                ExpressInfo expressInfo = expressService.getExpressInfo(expressId);
                if (expressInfo == null || (expressInfo != null && expressInfo.getId() != userInfo.getId())) throw new MessageException("操作错误");
                data.put("expressInfo", expressInfo);
                data.put("defaultValList", JsonUtils.toJsonDF(areaService.getDefaultValueListByAreaId(expressInfo.getAreaId())));
            }
            return new ResponseBean(data);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("deleteExpress")
    public ResponseBean deleteExpress(int expressId){
        try{
            UserOperator userInfo = getUserInfo2();
            if(userInfo == null) throw new MessageException("请您先登录");
            expressService.newDeleteExpress(userInfo.getId(), expressId);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }


    @RequestMapping("getOrderActivityIndex")
    public String getArticles(){
        return "reservation_order";
    }

    @RequestMapping("getOrderActivitys")
    public String getActivitys(OrderInputView orderInputView, Model model){
        try{
            orderInputView.setId(getUserInfo2().getId());
            PageBean pageBean = orderService.getOrderActivitys(orderInputView);
            model.addAttribute("pageBean", pageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "reservation_order_child";
    }

}
